package com.creative.accountsdk.holder;

import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.creative.accountsdk.DataCenter;
import com.creative.accountsdk.DialogUtils;
import com.creative.accountsdk.R;
import com.creative.accountsdk.UserData;
import com.creative.accountsdk.net.XHttpHandler;
import com.creative.accountsdk.net.account.ChildUnbindParentProtocol;
import com.creative.accountsdk.net.account.UserInfoProtocol;

import org.json.JSONObject;

/**
 * Created by wang_qian on 2015/11/30.
 */
public class ChildUnbindParentHolder extends BaseHolder {

    private TextView   tvInfo;
    private Button     btnUnbind;
    private String     parentName;
    private PopupWindow   unbindWin = null;



    public ChildUnbindParentHolder(String parentname) {
        parentName = parentname;
    }


    @Override
    protected void initViews() {
        tvInfo = (TextView)viewRoot.findViewById(R.id.tvbinded);
        btnUnbind = (Button)viewRoot.findViewById(R.id.btnunbind);

        tvInfo.setText(activity.getResources().getString(R.string.unbind_parent_hint1) + parentName +
        activity.getResources().getString(R.string.unbind_parent_hint2));

        btnUnbind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopupWindow(btnUnbind);
            }
        });

    }

    @Override
    public int getLayoutResID() {
        return R.layout.child_unbind_parent_layout;
    }

    private void unbindParent() {
        progress = DialogUtils.createProgress(activity);
        progress.setCancelable(false);
        progress.show();
        final ChildUnbindParentProtocol protocol = new ChildUnbindParentProtocol();
        protocol.debugUrl(DataCenter.getHttpClient());
        DataCenter.getHttpClient().post(protocol.getUrl(), protocol.getParams(), new XHttpHandler(activity, progress) {
            @Override
            public void onSuccess(int statusCode, cz.msebera.android.httpclient.Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                progress.cancel();
                if (protocol.parse(response, activity)) {
                    UserData.getInstance().initParent(null);
                    UserData.getInstance().writeParent(activity, null);
                    if (unbindWin != null && unbindWin.isShowing()) {
                        unbindWin.dismiss();
                    }
                    activity.finish();
                }
            }
        });

    }


    private void showPopupWindow(View view) {

        if (unbindWin != null) {
            unbindWin.showAtLocation(viewRoot, Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, 0);
        } else {
            View contentView = activity.getLayoutInflater().from(activity).inflate(
                    R.layout.unbind_popup_layout, null);

            Button unbindbutton = (Button) contentView.findViewById(R.id.btnunbind);
            unbindbutton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    unbindParent();
                }
            });

            Button cancelBtn = (Button) contentView.findViewById(R.id.btncancel);
            cancelBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    unbindWin.dismiss();
                }
            });

            unbindWin = new PopupWindow(contentView,
                    (int) activity.getResources().getDimension(R.dimen.unbind_popwindow_width),
                    (int) activity.getResources().getDimension(R.dimen.unbind_popwindow_height), true);
            unbindWin.setBackgroundDrawable(new BitmapDrawable());
            unbindWin.setOutsideTouchable(true);

            unbindWin.setAnimationStyle(R.style.popwindow_anim_style);
            unbindWin.showAtLocation(viewRoot, Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, 0);
        }


    }
}


